/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.seatunnel.connectors.seatunnel.redis.source;

import org.apache.seatunnel.api.serialization.DeserializationSchema;
import org.apache.seatunnel.api.source.Collector;
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
import org.apache.seatunnel.common.utils.JsonUtils;
import org.apache.seatunnel.connectors.seatunnel.redis.client.RedisClient;
import org.apache.seatunnel.connectors.seatunnel.redis.config.RedisParameters;
import org.apache.seatunnel.connectors.seatunnel.redis.config.RedisSourceOptions;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public abstract class RedisRecordReader {
    protected final RedisParameters redisParameters;
    protected final DeserializationSchema<SeaTunnelRow> deserializationSchema;
    protected RedisClient redisClient;

    protected RedisRecordReader(
            RedisParameters redisParameters,
            DeserializationSchema<SeaTunnelRow> deserializationSchema,
            RedisClient redisClient) {
        this.redisParameters = redisParameters;
        this.deserializationSchema = deserializationSchema;
        this.redisClient = redisClient;
    }

    public void pollHashMapToNext(List<String> keys, Collector<SeaTunnelRow> output)
            throws IOException {
        List<Map<String, String>> values = redisClient.batchGetHash(keys);
        if (deserializationSchema == null) {
            for (Map<String, String> value : values) {
                output.collect(new SeaTunnelRow(new Object[] {JsonUtils.toJsonString(value)}));
            }
            return;
        }
        for (Map<String, String> recordsMap : values) {
            if (redisParameters.getHashKeyParseMode() == RedisSourceOptions.HashKeyParseMode.KV) {
                deserializationSchema.deserialize(
                        JsonUtils.toJsonString(recordsMap).getBytes(), output);
            } else {
                SeaTunnelRow seaTunnelRow =
                        new SeaTunnelRow(new Object[] {JsonUtils.toJsonString(recordsMap)});
                output.collect(seaTunnelRow);
            }
        }
    }

    public abstract void pollZsetToNext(List<String> keys, Collector<SeaTunnelRow> output)
            throws IOException;

    public abstract void pollSetToNext(List<String> keys, Collector<SeaTunnelRow> output)
            throws IOException;

    public abstract void pollListToNext(List<String> keys, Collector<SeaTunnelRow> output)
            throws IOException;

    public abstract void pollStringToNext(List<String> keys, Collector<SeaTunnelRow> output)
            throws IOException;
}
